package cn.net.yto.school.web.clazz;

import cn.net.yto.base.dao.entities.TAccount;
import cn.net.yto.base.dao.pagination.PaginationRequest;
import cn.net.yto.base.dao.pagination.PaginationResponse;
import cn.net.yto.base.shiro.util.ShiroUserHelper;
import cn.net.yto.base.util.RequestHandler;
import cn.net.yto.school.dao.entities.TbClazz;
import cn.net.yto.school.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/clazz")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @RequestMapping("query")
    public String query(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "clazz/clazz_init";
    }

    @RequestMapping("list")
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
        //model里面会放前台传进来的搜索参数

        //创建分页对象 paginationRequest
        //通过RequestHandler的buildPaginationRequest方法将request和model传进去
        PaginationRequest paginationRequest = RequestHandler.buildPaginationRequest(request, model);

        // 通过Service获取查询集合
        PaginationResponse<TbClazz> paginationResponse = this.clazzService.findAllClazzByPagination(paginationRequest);
        model.addAttribute("paginationResponse", paginationResponse);
        return "clazz/clazz_list";
    }

    @RequestMapping(value = {"add"}, method = {RequestMethod.GET})
    public String addClazz(HttpServletRequest request, HttpServletResponse response, Model model) {

        return "clazz/clazz_add";
    }

    @RequestMapping(value = {"add"}, method = {RequestMethod.POST})
    public String addClazzHandler(HttpServletRequest request, HttpServletResponse response, Model model) {

        TbClazz clazz = (TbClazz) RequestHandler.createBean(request, new TbClazz());
//        String user = ShiroUserHelper.getCurrentShiroUser().getId() + "";
//        clazz.setActor(Integer.parseInt(user));
        TAccount account = new TAccount();
        account.setId(ShiroUserHelper.getCurrentShiroUser().getId());
        clazz.setActor(account);

        this.clazzService.saveClazz(clazz);

        return "redirect:/clazz/query";
    }

    @RequestMapping(value = {"delete/{clsId}"}, method = {RequestMethod.GET})
    public String deleteClazzHandler(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable("clsId") Integer clsId) {
        this.clazzService.deleteClazz(clsId);

        return "redirect:/clazz/query";
    }

    @RequestMapping(value = {"edit/{clsId}"}, method = {RequestMethod.GET})
    public String editClazz(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable("clsId") Integer clsId) {
        model.addAttribute("clazz", this.clazzService.findOneByClazzId(clsId));

        return "clazz/clazz_edit";
    }

    @RequestMapping(value = {"edit"}, method = {RequestMethod.POST})
    public String editClazzHandler(HttpServletRequest request, HttpServletResponse response, Model model) {

        TbClazz clazz = (TbClazz) RequestHandler.createBean(request, new TbClazz());

        //增加最后一次操作员字段
//        String user = ShiroUserHelper.getCurrentShiroUser().getId() + "";
//        clazz.setActor(Integer.parseInt(user));
        TAccount account = new TAccount();
        account.setId(ShiroUserHelper.getCurrentShiroUser().getId());
        clazz.setActor(account);

        this.clazzService.saveClazz(clazz);

        return "redirect:/clazz/query";
    }
}
